/**
 *
 */
package de.cbf.cam.dsa.facet.category.editor;

import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.widgets.Control;

import de.cbf.cam.dsa.facet.category.FacetCategory;
import de.cbf.util.entity.facet.edit.composites.factorys.AbstractFacetSectionFactory;
import de.cbf.util.entity.facet.edit.controller.AbstractControllerEditFacet;
import de.cbf.util.entity.facet.entities.FacetOfEntity;
import de.cbf.util.ui.listener.IUiChangeListener;
import de.cbf.util.ui.listener.UiChangeEvent;

/**
 * @author salter
 *
 */
public class ControllerEditorFacetCategory extends AbstractControllerEditFacet<FacetCategory> {
	private static final Log logger = LogFactory.getLog(ControllerEditorFacetCategory.class);

	public static final String TEXT_STATE_EVENT_CategoryName_WARNING = FacetCategory.TEXT_UNSET_CATEGORY_NAME;

	protected static StateEvent STATE_EVENT_CategoryName_WARNING	= new StateEvent(TEXT_STATE_EVENT_CategoryName_WARNING, StateEvent.STATE_WARNING);
	
	public static final String CTRL_EVT_CODE_CATEGORY_CHANGE = "change_facet_category";
	
	protected CompositeEditCategory compositeEditCategory;

	protected Class<? extends FacetOfEntity> activeBaseFacet;

	/**
	 *
	 */
	public ControllerEditorFacetCategory() {
		super();
	}

	@Override
	protected void init() {
		super.init();
		pclFacetCategory = new PCLFacetCategory();
		uilFacetCategory = new UILFacetCategory();
	}



	@Override
	protected AbstractFacetSectionFactory createSectionFactory() {
		return new SectionFactoryCategory();
	}

	@Override
	protected FacetCategory createNewFacet() {
		FacetCategory facet = new FacetCategory();
		facet.setActiveBaseFacet(activeBaseFacet);
		return facet;
	}

	@Override
	public Class<FacetCategory> getFacetClass() {
		return FacetCategory.class;
	}

	public void handleChangeCategory() {
		fireControllerEvent(this,CTRL_EVT_CODE_CATEGORY_CHANGE);
	}

	public void handleRemoveCategory() {
		facet.setParentCategoryName("");
		facet.setParentCategoryUUID(null);
		facet.setImageDesc(null);
	}

	public void updateParentCategoryName(String newParentCategoryName) {
		if(compositeEditCategory != null){			
			if( ! newParentCategoryName.equals(FacetCategory.UNSET_CATEGORY_NAME)){				
				compositeEditCategory.setCategoryText(newParentCategoryName);
				removeStateEvent(STATE_EVENT_CategoryName_WARNING);
			}else{
				compositeEditCategory.setCategoryText("");
				addStateEvent(STATE_EVENT_CategoryName_WARNING);
			}	
		}		
	}

	@Override
	protected void updateAll(FacetCategory facet) {
		if(facet != null){
			updateParentCategoryName(facet.getParentCategoryName());
		}else{
			updateParentCategoryName(FacetCategory.UNSET_CATEGORY_NAME);
		}		
	}
	
	@Override
	protected void preSetSection() {
		super.preSetSection();
		if(section != null){
			Control client = section.getClient();
			if (client instanceof CompositeEditCategory) {
				compositeEditCategory = (CompositeEditCategory) client;
				deRegisterUIListeners(compositeEditCategory);
			}else{
				logger.error("wrong client in section: expected "+CompositeEditCategory.class.getSimpleName()+
						" got: "+client.getClass().getSimpleName());
			}
		}
	}

	@Override
	protected void postSetSection() {
		super.postSetSection();
		Control client = section.getClient();
		if (client instanceof CompositeEditCategory) {
			compositeEditCategory = (CompositeEditCategory) client;
			registerUIListeners(compositeEditCategory);
		}else{
			logger.error("wrong client in section: expected "+CompositeEditCategory.class.getSimpleName()+
					" got: "+client.getClass().getSimpleName());
		}
	}

	protected void registerUIListeners(CompositeEditCategory compositeCategory) {
		compositeCategory.registerUIListener(uilFacetCategory);
	}

	protected void deRegisterUIListeners(CompositeEditCategory compositeCategory) {
		compositeCategory.deRegisterUIListener(uilFacetCategory);
	}

	@Override
	protected void preSetNewFacet() {
		super.preSetNewFacet();
		if(facet != null){
			deRegisterModelListeners(facet);
		}
	}

	@Override
	protected void postSetNewFacet() {
		super.postSetNewFacet();
		if(facet != null){
			registerModelListeners(facet);			
		}
	}



	protected void registerModelListeners(FacetCategory facet) {
		facet.getPropertyChangeSupport().addPropertyChangeListener(pclFacetCategory);
	}

	protected void deRegisterModelListeners(FacetCategory facet) {
		facet.getPropertyChangeSupport().removePropertyChangeListener(pclFacetCategory);
	}

	public Class<? extends FacetOfEntity> getActiveBaseFacet() {
		return activeBaseFacet;
	}

	public void setActiveBaseFacet(Class<? extends FacetOfEntity> activeBaseFacet) {
		this.activeBaseFacet = activeBaseFacet;
	}

	protected UILFacetCategory uilFacetCategory;
	protected class UILFacetCategory implements IUiChangeListener{
		@Override
		public void uiChanged(UiChangeEvent event) {
		}

		@Override
		public void commandIssued(int index, Object... parameters) {
			switch (index) {
				case CompositeEditCategory.CMD_CATEGORY_ADD:
					handleChangeCategory();
					break;
				case CompositeEditCategory.CMD_CATEGORY_DEL:
					handleRemoveCategory();
					break;
				default:
					logger.error("unknown commandIndex: "+index);
					break;
			}
		}
	}

	protected PCLFacetCategory pclFacetCategory;
	protected class PCLFacetCategory implements PropertyChangeListener{

		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idx_Evt = (IndexedPropertyChangeEvent) evt;
				switch (idx_Evt.getIndex()) {
					case FacetCategory.PI_PARENT_CATEGORY_NAME:{
						String newParentName = (String)idx_Evt.getNewValue();
						updateParentCategoryName(newParentName);
						break;
					}
//					case FacetCategory.PI_PARENT_CATEGORY_UUID:{
//						UUID newParentUUID = (UUID)idx_Evt.getNewValue();
//						updateParentCategoryUUID(newParentUUID);
//						break;
//					}
					default:
						break;
				}
			}
		}
	}
	






}
